package com.linkai.onlinetool.util;


/*
 *       /*
 *       2021年7月7日16:13:14
 *       测试用Java发送手机验证码短信
 *       使用的是阿里云的API ，是从阿里云市场购买的专门提供这种服务的产品，调用他们的API
 *       -----------------------——》》》已经完成封装可以使用
 * */

import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import java.util.Random;

public class SendMessageByALBB {
    private String phoneNumber;     // 手机号码用字符串来表示
    private long randomNumber;      // 生成的随机数

    public SendMessageByALBB() {
        // 如果没有给构造函数传递参数，就默认给自己的手机号码发送验证码
        this.phoneNumber = "15970819628";       // 自己的电信手机号码为：19970411540
    }

    public SendMessageByALBB(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public long getRandomNumber() {
        return randomNumber;
    }

    // 生成随机数
    public long myRandom() {
        Random random = new Random();
        long randNumber = random.nextInt(900000) + 100000;
        System.out.println(randNumber);
        this.randomNumber = randNumber;
        return randNumber;
    }

    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId("LTAI4FiNcQseapHSKnALVvJ1")
                // 您的AccessKey Secret
                .setAccessKeySecret("fLXxzNYZ4VZRl2QJPm1aee9z610FsU");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public void sendMessage() throws Exception {
        com.aliyun.dysmsapi20170525.Client client = SendMessageByALBB.createClient("accessKeyId", "accessKeySecret");
        // 签名一定要合法，否则会发送失败
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName("JackLin的博客")
                .setTemplateCode("SMS_175405719")
                .setTemplateParam("{\"code\":\"" + String.valueOf(myRandom()) + "\"}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendResp = client.sendSms(sendSmsRequest);
        String code = sendResp.body.code;
        if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
            System.out.println("错误信息: " + sendResp.body.message + "");
            return;
        }
    }


    // 用来测试
    public static void main(String[] args_) throws   Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.dysmsapi20170525.Client client = SendMessageByALBB.createClient("accessKeyId", "accessKeySecret");
        // 签名一定要合法，否则会发送失败
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers("15970819628")
                .setSignName("JackLin的博客")
                .setTemplateCode("SMS_175405719")
                .setTemplateParam("{\"code\":\"" + "123456" + "\"}");
        // 复制代码运行请自行打印 API 的返回值
        SendSmsResponse sendResp = client.sendSms(sendSmsRequest);
        String code = sendResp.body.code;
        if (!com.aliyun.teautil.Common.equalString(code, "OK")) {
            System.out.println("错误信息: " + sendResp.body.message + "");
            return;
        }


    }
}


